#include "spriteAnima.h"

void spriteAnima::update(float dt)
{
    if(isFinished_)return;
    frameTime_+= dt;
    if (frameTime_ >= 1.0f / fps_){
        currentFrame_++;
        if (currentFrame_ >= totalframe_){
            currentFrame_ = 0;
            if(!isLoop_){
                isFinished_ = true;
            }
        }
        frameTime_ = 0.0f;
    }
    data_.src.x = data_.src.w * currentFrame_;
}

void spriteAnima::setData(const sprite_data &data)
{
    data_ = data;
    totalframe_ = data_.src.w / data_.src.h;
    data_.src.w = data_.src.h;
    size_ = glm::vec2(data_.src.w, data_.src.h);
}

spriteAnima *spriteAnima::createAnima(screen *parent,const char* path, float scale,
    const char *name,Anchor anchro)
{
    auto anima = new spriteAnima();
    anima->init();
    anima->setAnchor(anchro);
    anima->setData(sprite_data(path));
    anima->setScale(scale);
    anima->name_ = name;
    anima->setParent(parent);
    if(parent)
        parent->addChild(anima);
    return anima;
}
